package com.lxmes.device.controller;

import com.lxmes.common.annotation.Log;
import com.lxmes.common.core.controller.BaseController;
import com.lxmes.common.core.domain.AjaxResult;
import com.lxmes.common.core.domain.entity.SysDept;
import com.lxmes.common.core.page.TableDataInfo;
import com.lxmes.common.enums.BusinessType;
import com.lxmes.common.utils.poi.ExcelUtil;
import com.lxmes.device.domain.Device;
import com.lxmes.device.domain.DeviceType;
import com.lxmes.device.service.IDeviceService;
import com.lxmes.device.service.IDeviceTypeService;
import com.lxmes.system.service.ISysDeptService;
import com.lxmes.system.service.ISysDictDataService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备列表Controller
 * 
 * @author ruoyi
 * @date 2022-04-13
 */
@Api(tags = "设备管理")
@RestController
@RequestMapping("/device/device")
public class DeviceController extends BaseController
{
    @Autowired
    private IDeviceService deviceService;
    @Autowired
    private IDeviceTypeService deviceTypeService;
    @Autowired
    private ISysDictDataService dataService;
    @Autowired
    private ISysDeptService deptService;

    /**
     * 查询设备列表列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:list')")
    @GetMapping("/list")
    public TableDataInfo list(Device device)
    {
        startPage();
        List<Device> list = deviceService.selectDeviceList(device);
        return getDataTable(list);
    }
    /**
     * 查询设备列表列表
     */
    @ApiOperation(value="设备列表")
    //查询条件
    @ApiImplicitParams({
            @ApiImplicitParam(name = "queryParams", value = "设备名称、设备编码", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deviceType", value = "设备类别", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "useDept", value = "使用部门", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deviceName", value = "设备名称", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deviceCode", value = "设备编码", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deviceModel", value = "规格型号", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "devicePosition", value = "设备位置", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "operator", value = "操作人员", paramType="query", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "deviceIsFlag", value = "设备标记", paramType="query",dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "useStatus", value = "设备状态", paramType="query",  dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "pageSize", value = "分页大小",  required = true,paramType="query",  dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "pageNum", value = "当前页数",  required = true,paramType="query",   dataType = "int", dataTypeClass = Integer.class)
    })
    @ApiResponses({@ApiResponse(code=200,message = "0k",response = Device.class)})
    @PreAuthorize("@ss.hasPermi('mobile:device:list')")
    @GetMapping("/apiList")
    public TableDataInfo apiList(
                                 @RequestParam(value ="queryParams",required = false) String queryParams,
                                 @RequestParam(value ="deviceType",required = false) String deviceType,
                                 @RequestParam(value="useDept",required = false) String useDept,
                                 @RequestParam(value="deviceName",required = false) String deviceName,
                                 @RequestParam(value="deviceCode",required = false) String deviceCode,
                                 @RequestParam(value="deviceModel",required = false) String deviceModel,
                                 @RequestParam(value="devicePosition",required = false) String devicePosition,
                                 @RequestParam(value="operator",required = false) String operator,
                                 @RequestParam(value="deviceIsFlag",required = false) String deviceIsFlag,
                                 @RequestParam(value="useStatus",required = false) String useStatus)

    {
        startPage();
        Device device=new Device();
        device.setQueryParams(queryParams);
        device.setDeviceType(deviceType);
        device.setUseDept(useDept);
        device.setDeviceName(deviceName);
        device.setDeviceCode(deviceCode);
        device.setDeviceModel(deviceModel);
        device.setDevicePosition(devicePosition);
        device.setOperator(operator);
        device.setDeviceIsFlag(deviceIsFlag);
        device.setUseStatus(useStatus);
        List<Device> list = deviceService.selectDeviceApiList(device);
        return getDataTable(list);
    }
    /**
     * 导出设备列表列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:export')")
    @Log(title = "设备列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Device device)
    {
        List<Device> list = deviceService.selectDeviceList(device);
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.exportExcel(response, list, "设备列表数据","设备列表数据");
    }
    @Log(title = "设备列表", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('device:device:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        List<Device> deviceList = util.importExcel(file.getInputStream(),1);
        String operName = getUsername();
        String message = deviceService.importDevice(deviceList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Device> util = new ExcelUtil<Device>(Device.class);
        util.importTemplateExcel(response, "设备列表","设备列表数据");

    }
    /**
     * 获取设备列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('device:device:query')")
    @GetMapping(value = "/{deviceId}")
    public AjaxResult getInfo(@PathVariable("deviceId") Long deviceId)
    {
        return AjaxResult.success(deviceService.selectDeviceByDeviceId(deviceId));
    }

    /**
     * 新增设备列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:add')")
    @Log(title = "设备列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Device device) throws Exception {
        return toAjax(deviceService.insertDevice(device));
    }
    /**
     * 新增API设备列表
     */
    @ApiOperation(value="新增设备")
    @Log(title = "设备列表", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('mobile:device:add')")
    @PostMapping("/apiAdd")
    public AjaxResult apiAdd(@RequestBody Device device) throws Exception {
        return toAjax(deviceService.insertDevice(device));
    }
    @ApiOperation(value="获取设备类别")
    @PreAuthorize("@ss.hasPermi('mobile:device:query')")
    @GetMapping("/getApiDeviceType")
    public AjaxResult getDeviceType(){
        DeviceType deviceType=new  DeviceType();
        return AjaxResult.success(deviceTypeService.selectDeviceTypeList(deviceType));
    }
    @ApiOperation(value="获取设备标记")
    @GetMapping("/getApiDeviceIsFlag")
    public AjaxResult getDeviceIsFlag(){
        return AjaxResult.success( dataService.selectDictDataByType("device_is_flag"));
    }
    @ApiOperation(value="获取设备使用状况")
    @GetMapping("/getApiDeviceUseStatus")
    public AjaxResult getDeviceUseStatus(){
        return AjaxResult.success( dataService.selectDictDataByType("device_use_status"));
    }

    /**
     * 获取设备使用部门
     */
    @ApiOperation(value="获取设备使用部门")
    @GetMapping("/getApiDeviceUseDept")
    public AjaxResult getDeviceUseDept()
    {
        SysDept dept=new SysDept();
        List<SysDept> depts = deptService.selectDeptList(dept);
        return AjaxResult.success(deptService.buildDeptTreeSelect(depts));
    }
    /**
     * 获取设备列表详细信息
     */
    @ApiOperation(value="获取设备明细")
    @ApiImplicitParam(name = "deviceCode", value = "设备编码", required = true, dataType = "String", paramType = "path", dataTypeClass = String.class)
    @GetMapping(value = "/getApiDevice/{deviceCode}")

    public AjaxResult getApiDevice(@PathVariable("deviceCode") String deviceCode)
    {
        return AjaxResult.success(deviceService.selectDeviceByDeviceCode(deviceCode));
    }
    /**
     * 获取设备列表详细信息
     */
    @GetMapping(value = "/getDeviceByDeviceCode/{deviceCode}")
    public AjaxResult getDeviceByDeviceCode(@PathVariable("deviceCode") String deviceCode)
    {
        return AjaxResult.success(deviceService.selectDeviceByDeviceCode(deviceCode));
    }

    /**
     * 修改设备列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:edit')")
    @Log(title = "设备列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Device device) throws Exception {
        return toAjax(deviceService.updateDevice(device));
    }

    /**
     * 删除设备列表
     */
    @PreAuthorize("@ss.hasPermi('device:device:remove')")
    @Log(title = "设备列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{deviceIds}")
    public AjaxResult remove(@PathVariable Long[] deviceIds)
    {
        return toAjax(deviceService.deleteDeviceByDeviceIds(deviceIds));
    }
    /*重新生成获取二维码*/
    @PreAuthorize("@ss.hasPermi('device:device:create')")
    @Log(title = "设备列表", businessType = BusinessType.DELETE)
    @GetMapping("/createDeviceEwm/{deviceIds}")
    public AjaxResult createDeviceEwm(@PathVariable Long[] deviceIds) throws Exception {
        return toAjax(deviceService.createDeviceEwm(deviceIds));
    }
}
